﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCControl;

namespace Cost.Controllers
{
    public class MaterialProcessPickingController : Controller
    {
        #region 构造函数
        private readonly ICostRepository<MaterialProcess> materialprocessRepository;
        private readonly ICostRepository<MaterialProcessItem> materialprocessitemRepository;
        private readonly ICostRepository<Storage> storageRepository;
        private readonly ICostRepository<StockRecord> stockrecordRepository;
        private const string ListState = "Draft";
        private const string State = "Picking";
        private const string StockType = "加工领料";
        public MaterialProcessPickingController(ICostRepository<MaterialProcess> materialprocessRepository,
            ICostRepository<MaterialProcessItem> materialprocessitemRepository,
            ICostRepository<Storage> storageRepository,
            ICostRepository<StockRecord> stockrecordRepository)
        {
            this.materialprocessRepository = materialprocessRepository;
            this.materialprocessitemRepository = materialprocessitemRepository;
            this.storageRepository = storageRepository;
            this.stockrecordRepository = stockrecordRepository;
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = materialprocessRepository.GetPageable(pager, m => m.OrderState == ListState, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult Edit(string Id)
        {
            var data = materialprocessRepository.Find(Id) ?? new MaterialProcess();
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(string Id, IList<MaterialProcessItem> MaterialList)
        {
            var data = materialprocessRepository.Find(Id) ?? new MaterialProcess();
            data.OrderState = State;
            materialprocessRepository.Save(data);

            foreach (var childitem in MaterialList)
            {
                var childdata = materialprocessitemRepository.Find(childitem.Id);
                childdata.ActualPickCount = childitem.ActualPickCount;
                childdata.State = State;
                materialprocessitemRepository.Save(childdata);

                var Sordate = storageRepository.Find(s => s.MaterialId == childitem.RawMaterialId);
                if (Sordate != null)
                {
                    Sordate.ActualSorage -= childitem.ActualPickCount;
                    storageRepository.Save(Sordate);
                }

                //保存库存记录
                StockRecord StockRecorddate = new StockRecord()
                {
                    Id = Guid.NewGuid().ToString(),
                    MaterialId = childitem.MaterialId,
                    Type = StockType,
                    Count = childitem.ActualPickCount,
                };
                stockrecordRepository.Insert(StockRecorddate);
            }
            return Json(AjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult MaterialList(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = materialprocessitemRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        public ActionResult EditList()
        {
            return View();
        }

        [HttpPost]
        public JsonResult EditList(string orderId, nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = materialprocessitemRepository.GetPageable(pager, d => d.OrderId == orderId, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        [HttpPost]
        public JsonResult Picking(string Id)
        {
            var data = materialprocessRepository.Find(Id) ?? new MaterialProcess();
            data.OrderState = State;
            materialprocessRepository.Save(data);

            var MaterialList = materialprocessitemRepository.FindAll(m => m.OrderId == Id);

            foreach (var childitem in MaterialList)
            {
                var childdata = materialprocessitemRepository.Find(childitem.Id);
                childdata.ActualPickCount = childitem.ActualPickCount;
                childdata.State = State;
                materialprocessitemRepository.Save(childdata);

                var Sordate = storageRepository.Find(s => s.MaterialId == childitem.RawMaterialId);
                if (Sordate != null)
                {
                    Sordate.ActualSorage -= childitem.ActualPickCount;
                    storageRepository.Save(Sordate);
                }

                //保存库存记录
                StockRecord StockRecorddate = new StockRecord()
                {
                    Id = Guid.NewGuid().ToString(),
                    MaterialId = childitem.MaterialId,
                    Type = StockType,
                    Count = childitem.ActualPickCount,
                };
                stockrecordRepository.Insert(StockRecorddate);
            }
            return Json(AjaxMessage.Successful);
        }
    }
}
